﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MSWord = Microsoft.Office.Interop.Word;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using NPOI.XWPF.UserModel;
using ZenonImportLanguage;
using Microsoft.Office.Interop.Word;

namespace WordExcel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dia = new OpenFileDialog();
            dia.Filter = "文件（*.docx）|*.docx";
            if (dia.ShowDialog() == DialogResult.OK)
            {
                tbxWord.Text = dia.FileName;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog dia = new OpenFileDialog();
            dia.Filter = "工作簿文件（*.xlsx）|*.xlsx|97-2003工作簿文件（*.xls）|*.xls";
            if (dia.ShowDialog() == DialogResult.OK)
            {
                tbxExcel.Text = dia.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            NPOIExcelHelper excel = new NPOIExcelHelper();

            excel.ReadExcelInit(tbxExcel.Text);
            List<StudentSituationEntity> stDatas = new List<StudentSituationEntity>();
            Dictionary<string, string> dict1 = new Dictionary<string, string>
                    {
                        {"Name","署名"},
                        {"Sex","性别"},
                        {"Code","学号"},
                         {"FamilyMembers","家庭常住人员"},
                          {"PickupAndDrop","接送人员"},
                            {"Fooder","饮居起食负责者"},
                           {"HomeSchool","家校沟通、学业跟进"},
                            {"Changes","近期重大变化"},
                             {"Problem","家庭教育方面困惑"},
                              {"IsHaveHome","孩子有无独立卧室"},
                               {"IsHaveDesk","孩子有无独立书桌"},
                                {"Factors","家庭学习干扰因素"},
                                 {"Personality","孩子个性情况"},
                                  {"Speciality","孩子兴趣特长"},
                                   {"Read","孩子阅读情况"},
                                    {"PhysicalCulture","体育锻炼及在家劳动情况"},
                                     {"Development","身心发育情况"},
                                      {"Disease","特殊体质、疾病情况"},
                    };

            excel.SetSheetIndex(0);
            stDatas = excel.ExcelToEntity<StudentSituationEntity>(dict1,0);
            excel.Dispose();
            if (stDatas.Count > 0)
            {
                string path = tbxWord.Text.Trim();
                WordHelper word = new WordHelper();
                word.OpenDocument(path);
                progressBar1.Maximum = stDatas.Count;
                int index = 0;
                Random rand = new Random();
                foreach (var item in stDatas)
                {
                    if (word.SetTable(1))
                    {
                        index++;
                        word.SetCellValue(1, 2, item.Name);
                        word.SetCellValue(1, 4, item.Sex);
                        word.SetCellValue(1, 6, item.Code);
                        string dd = "2023/09/" + rand.Next(7, 26).ToString().PadLeft(2,'0');
                        word.SetCellValue(1,8,dd );   //家访日期
                        word.SetCellValue(2, 2, "实地家访");  //家访形式
                      //  word.SetCellValue(4, 2, item.Code);  //住址
                        word.SetCellValue(4, 4, item.FamilyMembers);  //常住人员
                        word.SetCellValue(5, 3, item.PickupAndDrop);  //接送人员
                        word.SetCellValue(5, 6, item.HomeSchool);  //家校
                        word.SetCellValue(6, 3, item.Fooder);  //饮食起居
                        word.SetCellValue(6, 6, item.HomeSchool);  //学业
                        word.SetCellValue(7, 2, item.Changes);  ///近期重大变化
                        word.SetCellValue(7, 4, item.Problem);  //家庭教育方面困惑
                        word.SetCellValue(9, 2, item.IsHaveHome);  //独立卧室
                        word.SetCellValue(9,4, item.IsHaveDesk);   //独立书桌
                        word.SetCellValue(9, 6, item.Factors);   //干扰因素
                        word.SetCellValue(11,2, item.Personality);  //个性
                        word.SetCellValue(11, 4, item.Speciality);  //兴趣
                        word.SetCellValue(12, 2, item.Read);  //阅读
                        word.SetCellValue(12, 4, item.PhysicalCulture);  //体锻及
                        word.SetCellValue(13, 2, item.Development);  //身心发育情况
                        word.SetCellValue(13, 4, item.Disease);  //特殊体质、疾病情况
                        var arrays = path.Split('.');
                        string newPath = arrays[0]+"-"+item.Code.PadLeft(2,'0')+"-"+item.Name+"."+arrays[1];
                        word.SaveDocument(newPath);
                        progressBar1.Value = index;
                    }
                }
                word.CloseDocument();
                word.Quit();
                MessageBox.Show("共生成:"+ index.ToString()+"个文档！");
            }




         
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog dia = new OpenFileDialog();
            dia.Filter = "文件（*.docx）|*.docx";
            if (dia.ShowDialog() == DialogResult.OK)
            {
                tbxTest.Text = dia.FileName;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //string path = tbxTest.Text.Trim();
            //WordHelper word = new WordHelper();
            //word.OpenDocument(path);
            //var string1 = word.GetParagraphText(2);
            //var string2 = word.GetParagraphText(3);
            //word.SetParagraphText(2, "姓名：张三   性别： 男  \r");
            //word.SetParagraphText(3, "年龄：21\r");
            //word.AddParagraph("天行健，君子以自强不息！");
            //word.AddParagraph("地势坤，君子以厚德载物！");
            //word.SaveDocument(path);
            //word.CloseDocument();
            //word.Quit();
            //MessageBox.Show("OK");
            NPOIWordHelper word = new NPOIWordHelper();
            try
            {
               
                string path = tbxTest.Text.Trim();
                word.OpenDocument(path);
                word.SetTable(0);
                word.SetCellValue(0, 1, "1", "楷体", 12, true);
                word.SetCellValue(0, 3, "2", "楷体", 12, true);
                word.SetCellValue(0, 5, "3", "楷体", 12, true);
                word.SetCellValue(1, 1, "4", "楷体", 12, true);
                word.SetCellValue(3, 1, "5", "楷体", 12, true);
                word.SetCellValue(3, 3, "6", "楷体", 12, true);
                word.SetCellValue(4, 2, "7", "楷体", 12, true);
                word.SetCellValue(4, 5, "8", "楷体", 12, true);
                word.SetCellValue(4, 3, "QQ", "楷体", 12, true);
                word.SetCellValue(4, 4, "9", "楷体", 12, true);
                word.SetCellValue(4, 5, "10", "楷体", 12, true);
                word.SetCellValue(6, 1, "12", "楷体", 12, true);


                //var a1 = word.GetParagraphText(0);
                //var a2 = word.GetParagraphText(1);
                //word.AddParagraph("天行健，君子以自强不息！");
                word.SaveToFile(path);
            }
            catch (Exception ex)
                {

            }
            finally
            {
                word.CloseDocument();
                MessageBox.Show("OK");
            }

        }
    }
}
